﻿Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      FillDatabaseCombo()
   End Sub

   Private Sub morphButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles morphButton.Click
      Dim metadataPath = IO.Path.Combine(metadataTextBox.Text, databaseComboBox.Text)
      Dim dbMetadata = IO.Path.Combine(metadataPath, "DBMetadata.xml")
      Dim ormFile = IO.Path.Combine(metadataPath, "ORMFile.xml")
      KadGen.LogicWrappers2008.MorphMetadata.MorphForObjects(dbMetadata, ormFile)
   End Sub

   Private Sub extractMetadataButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles extractMetadataButton.Click
      Dim metadataPath = IO.Path.Combine(metadataTextBox.Text, databaseComboBox.Text)
      Dim dbMetadata = IO.Path.Combine(metadataPath, "DBMetadata.xml")
      KadGen.LogicWrappers2008.ExtractMetadata.Extract(Me.serverTextBox.Text, Me.databaseComboBox.Text)
   End Sub

   Private Sub FillDatabaseCombo()
      If Not String.IsNullOrEmpty(serverTextBox.Text) Then
         Dim connection = New Data.SqlClient.SqlConnection( _
                  "integrated security=SSPI" & _
                  ";data source=" & Me.serverTextBox.Text & _
                  ";persist security info=False" & _
                  ";initial catalog=Master")
         Try
            connection.Open()
            Dim ds = connection.GetSchema("Databases")
            Dim q = From row In ds.Rows.OfType(Of Data.DataRow)() _
                    Select Name = row(0).ToString() _
                    Order By Name
            databaseComboBox.Items.Clear()
            For Each dataName In q
               databaseComboBox.Items.Add(dataName)
            Next
            databaseComboBox.SelectedIndex = 0
         Catch
            connection.Close()
         End Try

      End If
   End Sub

End Class
